﻿function On_WindowLoad()
{
    var RenderCallback = function ()
    {
        return (
            window.requestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.oRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            function (callback)
            {
                window.setTimeout(callback, 1);
            }
        );
    }();
    var InitializeControls = function ()
    {
        // Resizable dialogs
        $('.resizable').resizable().draggable({ cancel: '.scrollable' });

        // Blocksize slider tooltip
        var tooltip = $('<div id="sldBlocksizeTooltip" class="slidertooltip" />').show();

        // Blocksize slider
        $("#sldBlocksize").slider({
            range: "min",
            value: 16,
            min: 1,
            max: 32,
            slide: function (event, ui) { tilemap.Set('blocksize', ui.value); tooltip.text(ui.value);}
        }).find(".ui-slider-handle").append(tooltip);

    }
    engine.Initialize(document.getElementById("canvas"));
    engine.PlaySound(engine.startupsound);
    window.requestAnimFrame = RenderCallback;
    window.onresize = engine.On_WindowResize.bind(engine);
    InitializeControls();
    On_Tick();

}

"use strict";

function On_Tick()
{
    if (window.useanimframe)
        window.requestAnimFrame(On_Tick);
    else
        window.setTimeout(On_Tick, 1);
    engine.Render();
}

function SetUpLogging()
{
    if (!window.console) window.console = {};
    if (!window.console.log) window.console.log = function () { };
    window.txtOutput = document.getElementById('txtDebug');
    var txtOutput = window.txtOutput;
    txtOutput.logctr = 0;
    txtOutput.lastlogline = '';
    txtOutput.logtext = '';
    window.log = function (s)
    {
        if (s != txtOutput.lastlogline)
        {
            txtOutput.lastlogline = s;
            if (txtOutput.logctr > 500)
            {
                txtOutput.logtext = s;
                txtOutput.logctr = 0;
            } else
            {
                txtOutput.logtext += '\n' + s;
                txtOutput.logctr += 1;
            }
            txtOutput.value = txtOutput.logtext;
            txtOutput.scrollTop = txtOutput.scrollHeight;
            window.console.log(s);
        }
    };
}

SetUpLogging();
window.useanimframe = false;
window.onload = On_WindowLoad;
window.onbeforeunload = function () { return "Are you sure you want to leave this page?"; };

